package net.huashitong.Indicators.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import com.yunpian.sdk.model.User;
import net.huashitong.Indicators.controller.UserInfoController;
import net.huashitong.Indicators.dataBean.CodeDataBean;
import net.huashitong.Indicators.entity.*;
import net.huashitong.until.Until;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

import static net.huashitong.Indicators.utils.DataFormateUntil.removeDuplicate;
import static net.huashitong.until.Until.*;

@Service
public class GetDataService {
    @Autowired
    private OneIndexService oneService;
    @Autowired
    private TwoIndexService twoService;
    @Autowired
    private ThreeIndexService threeService;

    @Autowired
    private UserInfoService service;
    @Autowired
    private IndexDataService indexservice;

    @Autowired
    private AreaCodeService areaService;

    @Autowired
    private IndexDataService indexDataService;

    @Autowired
    private GetDataProcessService getDataProcessService;

    private static Logger log = LoggerFactory.getLogger(GetDataService.class);


    /**
     * 厦门市月度数据
     *
     * @return
     */
    public Map<String, Object> getYueDuDataFromURL(String DataBase, String codetype, String areaType) {
        JSONObject jsonObject = null;
        JSONArray jsonArray1 = null;
        List<OneIndex> grandparent = new ArrayList<>();
        List<OneIndex> parentList = new ArrayList<>();
        List<OneIndex> childlist = new ArrayList<>();
        List<OneIndex> child4 = new ArrayList<>();
        List<OneIndex> child5 = new ArrayList<>();
       /* OneIndex twoindex =null;
        OneIndex threeindex=null;
        OneIndex index4 =null;*/
        String response = null; //获取厦门市月度指标
        try {
            response = Until.HttpGET(Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase, 60000, 60000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase);
        }
        jsonObject = JSONObject.parseObject(response);
        jsonArray1 = jsonObject.getJSONArray("returndata");
        /**  一级指标  **/
        for (int i = 0; i < jsonArray1.size(); i++) {
            OneIndex oneindex = new OneIndex();
            JSONObject object = (JSONObject) jsonArray1.get(i);
            oneindex.setCodeId(object.getString("code"));
            oneindex.setCodeName(object.getString("name"));
            oneindex.setCodeNode("1");
            oneindex.setAreaType(areaType);
            oneindex.setCodeType(codetype);
            grandparent.add(oneindex);
            /**** 二级指标 ****/
            String parentcode = null;
            try {
                parentcode = Until.HttpGET(Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + oneindex.getCodeId(), 60000, 60000);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("错误的接口：" + Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + oneindex.getCodeId());
            }
            JSONObject jsonObject2 = JSONObject.parseObject(parentcode);
            int returncode = jsonObject2.getIntValue("returncode");
            if (200 == returncode) {
                JSONArray jsonArray2 = jsonObject2.getJSONArray("returndata");
                if (jsonArray2.size() > 0) {  //开始遍历第二层数据
                    for (int j = 0; j < jsonArray2.size(); j++) {
                        OneIndex twoindex = new OneIndex();
                        JSONObject object2 = (JSONObject) jsonArray2.get(j);
                        twoindex.setCodeId(object2.getString("code"));
                        twoindex.setCodeName(object2.getString("name"));
                        twoindex.setParentCode(oneindex.getCodeId());
                        twoindex.setCodeType(codetype);
                        twoindex.setCodeNode("2");
                        twoindex.setAreaType(areaType);
                        parentList.add(twoindex);
                        /***  三级指标  ****/
                        String childCode = null;
                        try {
                            childCode = Until.HttpGET(Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + twoindex.getCodeId(), 60000, 60000);
                        } catch (IOException e) {
                            e.printStackTrace();
                            log.info("错误的接口：" + Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + twoindex.getCodeId());
                        }
                        JSONObject jsonObject3 = JSONObject.parseObject(childCode);
                        int returncode3 = jsonObject3.getIntValue("returncode");
                        if (returncode3 == 200) {
                            JSONArray jsonArray3 = jsonObject3.getJSONArray("returndata");
                            if (jsonArray3.size() > 0) {  //开始遍历第三层数据
                                for (int m = 0; m < jsonArray3.size(); m++) {
                                    OneIndex threeindex = new OneIndex();
                                    JSONObject object3 = (JSONObject) jsonArray3.get(m);
                                    threeindex.setCodeId(object3.getString("code"));
                                    threeindex.setCodeName(object3.getString("name"));
                                    threeindex.setParentCode(twoindex.getCodeId());
                                    threeindex.setCodeType(codetype);
                                    threeindex.setAreaType(areaType);
                                    threeindex.setCodeNode("3");
                                    childlist.add(threeindex);
                                    /*** 第四层  ***/
                                    String fourchild = null;
                                    try {
                                        fourchild = Until.HttpGET(Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + threeindex.getCodeId(), 600000, 600000);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                        log.info("接口： " + Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + threeindex.getCodeId());
                                    }
                                    JSONObject jsonObject4 = JSONObject.parseObject(fourchild);
                                    int returncode4 = jsonObject4.getIntValue("returncode");
                                    if (returncode4 == 200) {
                                        JSONArray jsonArray4 = jsonObject4.getJSONArray("returndata");
                                        if (jsonArray4.size() > 0) {
                                            for (int n = 0; n < jsonArray4.size(); n++) {
                                                OneIndex index4 = new OneIndex();
                                                JSONObject object4 = (JSONObject) jsonArray4.get(n);
                                                index4.setCodeId(object4.getString("code"));
                                                index4.setCodeName(object4.getString("name"));
                                                index4.setParentCode(threeindex.getCodeId());
                                                index4.setCodeType(codetype);
                                                index4.setAreaType(areaType);
                                                index4.setCodeNode("4");
                                                child4.add(index4);
                                                //市级年度数据过多，需要分开存储
                                                if ("N".equals(codetype) && "D".equals(areaType)) {
                                                    continue;
                                                }
                                                /*** 第五层  ***/
                                                String fivechild = null;
                                                try {
                                                    fivechild = Until.HttpGET(Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + index4.getCodeId(), 600000, 600000);
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                    log.info("接口： " + Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + threeindex.getCodeId());
                                                }
                                                JSONObject jsonObject5 = JSONObject.parseObject(fivechild);
                                                int returncode5 = jsonObject5.getIntValue("returncode");
                                                if (returncode5 == 200) {
                                                    JSONArray jsonArray5 = jsonObject5.getJSONArray("returndata");
                                                    if (jsonArray5.size() > 0) {
                                                        for (int k = 0; k < jsonArray5.size(); k++) {
                                                            OneIndex index5 = new OneIndex();
                                                            JSONObject object5 = (JSONObject) jsonArray5.get(k);
                                                            index5.setCodeId(object5.getString("code"));
                                                            index5.setCodeName(object5.getString("name"));
                                                            index5.setCodeNode("5");
                                                            index5.setAreaType(areaType); //地区标志
                                                            index5.setCodeType(codetype);
                                                            index5.setParentCode(index4.getCodeId());
                                                            child5.add(index5);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("one", grandparent);
        map.put("two", parentList);
        map.put("three", childlist);
        map.put("four", child4);
        map.put("five", child5);
        return map;
    }

    //单独处理市级年度指标数据
    public void getShiJiNianDuIndex(String DataBase, String codetype, String areaType) {
        List<OneIndex> child5 = new ArrayList<>();
        //查询4级指标，再通过4级指标查询5级指标
        Map<String, Object> map = new HashMap<>();
        List<OneIndex> oneIndices = oneService.getTwoNode(codetype, "4", areaType); //获取所有四级指标

        for (int i = 0; i < oneIndices.size(); i++) {

            /*** 第五层  ***/
            String fivechild = null;
            try {
                fivechild = Until.HttpGET(Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + oneIndices.get(i).getCodeId(), 600000, 600000);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("接口： " + Until.Ind_CODE_NAME_DB_URL + "db=" + DataBase + "&p=" + oneIndices.get(i).getCodeId());
            }
            JSONObject jsonObject5 = JSONObject.parseObject(fivechild);
            int returncode5 = jsonObject5.getIntValue("returncode");
            if (returncode5 == 200) {
                JSONArray jsonArray5 = jsonObject5.getJSONArray("returndata");
                if (jsonArray5.size() > 0) {
                    for (int k = 0; k < jsonArray5.size(); k++) {
                        OneIndex index5 = new OneIndex();
                        JSONObject object5 = (JSONObject) jsonArray5.get(k);
                        index5.setCodeId(object5.getString("code"));
                        index5.setCodeName(object5.getString("name"));
                        index5.setCodeNode("5");
                        index5.setAreaType(areaType); //地区标志
                        index5.setCodeType(codetype);
                        index5.setParentCode(oneIndices.get(i).getCodeId());
                        child5.add(index5);
                    }
                }
            }
            if (i % 400 == 0 || oneIndices.size() - 1 == i) {
                map.put("five", child5);
                this.saveIndexsInShiJiNianDu(map, "N", "D");
                child5.clear();
            }
        }
    }

    /**
     * 保存指标
     *
     * @param map
     */
    public void saveIndexs(Map<String, Object> map, String codeType, String areaType) {
        List<OneIndex> index1 = (List<OneIndex>) map.get("one");
        List<OneIndex> index2 = (List<OneIndex>) map.get("two");
        List<OneIndex> index3 = (List<OneIndex>) map.get("three");
        List<OneIndex> index4 = (List<OneIndex>) map.get("four");
        List<OneIndex> index5 = (List<OneIndex>) map.get("five");
        /**
         * 保存前先删除旧指标
         */
        oneService.deleteOneIndex(codeType, areaType);

        if (index1.size() > 0) {
            oneService.batchSave(index1);
        }
        if (index2.size() > 0) {
            oneService.batchSave(index2);
        }
        if (index3.size() > 0) {
            for (OneIndex dto : index3) {
                OneIndex temp = new OneIndex();
                BeanUtils.copyProperties(dto, temp);
                oneService.save(temp);
            }
            //oneService.batchSave(index3);
        }
        if (index4.size() > 0) {
            // oneService.batchSave(index4);
            for (OneIndex dto : index4) {
                OneIndex temp = new OneIndex();
                BeanUtils.copyProperties(dto, temp);
                oneService.save(temp);
            }
        }
        if (index5.size() > 0) {
            // oneService.batchSave(index5);
            for (OneIndex dto : index5) {
                OneIndex temp = new OneIndex();
                BeanUtils.copyProperties(dto, temp);
                oneService.save(temp);
            }
        }

    }

    public void saveIndexsInShiJiNianDu(Map<String, Object> map, String codeType, String areaType) {
        List<OneIndex> index5 = (List<OneIndex>) map.get("five");
//        /**
//         * 保存前先删除旧指标
//         */
//        oneService.deleteIndexByCodeNode(codeType, areaType,"5");
        if (index5.size() > 0) {
            for (OneIndex dto : index5) {
                OneIndex temp = new OneIndex();
                BeanUtils.copyProperties(dto, temp);
                oneService.save(temp);
            }
        }

    }

    /**
     * 保存月度数据
     */
    public void saveYueDuData(String db, String codetype, String areaType) {
        Map<String, String> exceptonCode = new HashMap<>();
        List<OneIndex> index2s = oneService.getTwoNode(codetype, "2", areaType); //获取所有二级指标
        if ("N".equals(codetype) && "D".equals(areaType)) {
            List<OneIndex> oneIndices = oneService.getTwoNode(codetype, "4", areaType); //获取所有四级指标
            index2s.addAll(oneIndices);
        }
        List<String> timeTypelist = null;
        String yearresponse = null;
        try {
            yearresponse = Until.HttpGET(Until.GET_DATA_TIME + "db=" + db, 15000, 15000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.GET_DATA_TIME + "db=" + db);
        }

        List<String> yearcodes = new ArrayList<>();
        JSONObject obj = JSONObject.parseObject(yearresponse);
        if (200 == obj.getIntValue("returncode")) {
            String year = "";
            JSONArray array = obj.getJSONArray("returndata");
            if (array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    year = ((JSONObject) array.get(i)).getString("code");
                    year = year.substring(0, 4);
                    yearcodes.add(year);
                }
            }
        }
        yearcodes = removeDuplicate(yearcodes);
//        List<String> yearList = yearcodes;                                        //所有年份
        List<String> yearList = Collections.singletonList(yearcodes.get(0));        //最新一年
        timeTypelist = Until.TimeTypes();
        for (String yearcode : yearList) {
            log.info("-------第" + yearcode + "--------------");
            JSONObject childObject = null;
            List<List<IndexData>> savedata = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> map2 = new HashMap<>();
            for (OneIndex item2 : index2s) {
                List<IndexData> LeijiList = new ArrayList<>();
                List<IndexData> dangqianList = new ArrayList<>();
                List<IndexData> ratioList = new ArrayList<>();
                List<IndexData> qimoList = new ArrayList<>();
                List<IndexData> tongbiList = new ArrayList<>();
                List<IndexData> tongbiList1 = new ArrayList<>();

                for (int i = 0; i < timeTypelist.size(); i++) //当期值，累计值，增速，期末值
                {
                    String typecode = timeTypelist.get(i);
                    //获取厦门月度数据
                    String childResponse = null;
                    try {
                        childResponse = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode, 12000, 12000);
                    } catch (IOException e) {
                        e.printStackTrace();
                        exceptonCode.put(item2.getCodeId(), item2.getCodeId());
                        log.info("接口： " + Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode);
                        map.put(item2.getCodeId(), item2.getCodeId());
                        log.info("未处理的code: " + item2.getCodeId());
                        continue;
                    }
                    childObject = JSONObject.parseObject(childResponse);
                    int returnCode = childObject.getIntValue("returncode");
                    if (returnCode == 200) {
                        JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                        if (childArry.size() > 0) {
                            for (int j = 0; j < childArry.size(); j++) {

                                IndexData datas = new IndexData();
                                JSONObject datanodes = (JSONObject) childArry.get(j);
                                String codes = datanodes.getString("code");
                                CodeDataBean bean = Until.spitString(codes);
                                datas.setCodeId(bean.getCodeId());
                                datas.setDatetime(bean.getDatetime());
                                datas.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                                datas.setCodeType(codetype);
                                datas.setAreaType(areaType);
                                JSONObject data = datanodes.getJSONObject("data");
                                String dataValue = data.getString("data");

                                if (Until.DANGQIZHI.equals(typecode)) {
                                    datas.setCurrentValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//当期值
                                    dangqianList.add(datas);
                                }

                                if (Until.LEIJIZHI.equals(typecode)) {
                                    datas.setCumulativeValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计值
                                    LeijiList.add(datas); //所有当前值
                                }
                                if (Until.LEIJIZENGSU.equals(typecode)) {
                                    datas.setCumulativeRatio(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计增速
                                    ratioList.add(datas);
                                }
                                if (Until.QIMOZHI.equals(typecode)) {
                                    datas.setQimoValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//期末值
                                    qimoList.add(datas);
                                }

                                if (Until.TONGBIZZ.equals(typecode)) {
                                    datas.setTongbiValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//比上年同期增长率
                                    tongbiList.add(datas);
                                }

                                if (Until.BI_SHAGN_NIAN.equals(typecode)) {
                                    datas.setTongbiValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//比上年同期增长率
                                    tongbiList1.add(datas);
                                }


                            }
                        }
                    } else {
                        map.put(item2.getCodeId(), item2.getCodeId());
                        log.info("未处理的code: " + item2.getCodeId());
                    }
                }
                List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
                if (tempList.size() == 1) {
                    savedata.add(tempList.get(0));
                } else if (tempList.size() == 2) {
                    List<IndexData> data1 = tempList.get(0);
                    List<IndexData> data2 = tempList.get(1);
                    for (IndexData one : data1) {
                        for (IndexData two : data2) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    savedata.add(data1);
                } else if (tempList.size() == 3) {
                    for (IndexData one : dangqianList) {
                        for (IndexData two : LeijiList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        for (IndexData two : ratioList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeRatio(two.getCumulativeRatio());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                            for (IndexData two : qimoList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCurrentValue(two.getQimoValue());
                                    continue;
                                }
                            }
                        }
                    }

                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCumulativeRatio()) && tongbiList.size() > 0) {
                            for (IndexData two : tongbiList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCumulativeRatio(two.getTongbiValue());
                                    continue;
                                }
                            }
                        }
                    }

                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCumulativeRatio()) && tongbiList1.size() > 0) {
                            for (IndexData two : tongbiList1) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCumulativeRatio(two.getTongbiValue());
                                    continue;
                                }
                            }
                        }
                    }
                    savedata.add(dangqianList);
                }

            }   //指标 for
            deleteDatas(yearcode, codetype, areaType); //删除历史数据 Y J
            Map<String, IndexData> resMap = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);

            //处理三级数据
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                List<OneIndex> remains = oneService.getChileNode(entry.getValue().toString(), codetype, areaType);
                if (ObjectUtils.isNotBlank(remains) && remains.size() > 0) {
                    saveNotcodeData(remains, db, timeTypelist, yearcode, codetype, areaType, resMap);
                }
            }
//             indexservice.deletRepeatData(codetype, areaType);
        }  // 年份 for
    }



    /**
     * 保存月度数据
     */
    public void saveYueDuDataAllYear(String db, String codetype, String areaType) {
        Map<String, String> exceptonCode = new HashMap<>();
        List<OneIndex> index2s = oneService.getTwoNode(codetype, "2", areaType); //获取所有二级指标
        if ("N".equals(codetype) && "D".equals(areaType)) {
            List<OneIndex> oneIndices = oneService.getTwoNode(codetype, "4", areaType); //获取所有四级指标
            index2s.addAll(oneIndices);
        }
        List<String> timeTypelist = null;
        String yearresponse = null;
        try {
            yearresponse = Until.HttpGET(Until.GET_DATA_TIME + "db=" + db, 15000, 15000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.GET_DATA_TIME + "db=" + db);
        }

        List<String> yearcodes = new ArrayList<>();
        JSONObject obj = JSONObject.parseObject(yearresponse);
        if (200 == obj.getIntValue("returncode")) {
            String year = "";
            JSONArray array = obj.getJSONArray("returndata");
            if (array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    year = ((JSONObject) array.get(i)).getString("code");
                    year = year.substring(0, 4);
                    yearcodes.add(year);
                }
            }
        }
        yearcodes = removeDuplicate(yearcodes);
        List<String> yearList = yearcodes;                                        //所有年份
//        List<String> yearList = Collections.singletonList(yearcodes.get(0));        //最新一年
        timeTypelist = Until.TimeTypes();
        for (String yearcode : yearList) {
            log.info("-------第" + yearcode + "--------------");
            JSONObject childObject = null;
            List<List<IndexData>> savedata = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> map2 = new HashMap<>();
            for (OneIndex item2 : index2s) {
                List<IndexData> LeijiList = new ArrayList<>();
                List<IndexData> dangqianList = new ArrayList<>();
                List<IndexData> ratioList = new ArrayList<>();
                List<IndexData> qimoList = new ArrayList<>();
                List<IndexData> tongbiList = new ArrayList<>();
                List<IndexData> tongbiList1 = new ArrayList<>();

                for (int i = 0; i < timeTypelist.size(); i++) //当期值，累计值，增速，期末值
                {
                    String typecode = timeTypelist.get(i);
                    //获取厦门月度数据
                    String childResponse = null;
                    try {
                        childResponse = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode, 12000, 12000);
                    } catch (IOException e) {
                        e.printStackTrace();
                        exceptonCode.put(item2.getCodeId(), item2.getCodeId());
                        log.info("接口： " + Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode);
                        map.put(item2.getCodeId(), item2.getCodeId());
                        log.info("未处理的code: " + item2.getCodeId());
                        continue;
                    }
                    childObject = JSONObject.parseObject(childResponse);
                    int returnCode = childObject.getIntValue("returncode");
                    if (returnCode == 200) {
                        JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                        if (childArry.size() > 0) {
                            for (int j = 0; j < childArry.size(); j++) {

                                IndexData datas = new IndexData();
                                JSONObject datanodes = (JSONObject) childArry.get(j);
                                String codes = datanodes.getString("code");
                                CodeDataBean bean = Until.spitString(codes);
                                datas.setCodeId(bean.getCodeId());
                                datas.setDatetime(bean.getDatetime());
                                datas.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                                datas.setCodeType(codetype);
                                datas.setAreaType(areaType);
                                JSONObject data = datanodes.getJSONObject("data");
                                String dataValue = data.getString("data");

                                if (Until.DANGQIZHI.equals(typecode)) {
                                    datas.setCurrentValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//当期值
                                    dangqianList.add(datas);
                                }

                                if (Until.LEIJIZHI.equals(typecode)) {
                                    datas.setCumulativeValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计值
                                    LeijiList.add(datas); //所有当前值
                                }
                                if (Until.LEIJIZENGSU.equals(typecode)) {
                                    datas.setCumulativeRatio(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计增速
                                    ratioList.add(datas);
                                }
                                if (Until.QIMOZHI.equals(typecode)) {
                                    datas.setQimoValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//期末值
                                    qimoList.add(datas);
                                }

                                if (Until.TONGBIZZ.equals(typecode)) {
                                    datas.setTongbiValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//比上年同期增长率
                                    tongbiList.add(datas);
                                }

                                if (Until.BI_SHAGN_NIAN.equals(typecode)) {
                                    datas.setTongbiValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//比上年同期增长率
                                    tongbiList1.add(datas);
                                }


                            }
                        }
                    } else {
                        map.put(item2.getCodeId(), item2.getCodeId());
                        log.info("未处理的code: " + item2.getCodeId());
                    }
                }
                List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
                if (tempList.size() == 1) {
                    savedata.add(tempList.get(0));
                } else if (tempList.size() == 2) {
                    List<IndexData> data1 = tempList.get(0);
                    List<IndexData> data2 = tempList.get(1);
                    for (IndexData one : data1) {
                        for (IndexData two : data2) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    savedata.add(data1);
                } else if (tempList.size() == 3) {
                    for (IndexData one : dangqianList) {
                        for (IndexData two : LeijiList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        for (IndexData two : ratioList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeRatio(two.getCumulativeRatio());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                            for (IndexData two : qimoList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCurrentValue(two.getQimoValue());
                                    continue;
                                }
                            }
                        }
                    }

                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCumulativeRatio()) && tongbiList.size() > 0) {
                            for (IndexData two : tongbiList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCumulativeRatio(two.getTongbiValue());
                                    continue;
                                }
                            }
                        }
                    }

                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCumulativeRatio()) && tongbiList1.size() > 0) {
                            for (IndexData two : tongbiList1) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCumulativeRatio(two.getTongbiValue());
                                    continue;
                                }
                            }
                        }
                    }
                    savedata.add(dangqianList);
                }

            }   //指标 for
            deleteDatas(yearcode, codetype, areaType); //删除历史数据 Y J
            Map<String, IndexData> resMap = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);

            //处理三级数据
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                List<OneIndex> remains = oneService.getChileNode(entry.getValue().toString(), codetype, areaType);
                if (ObjectUtils.isNotBlank(remains) && remains.size() > 0) {
                    saveNotcodeData(remains, db, timeTypelist, yearcode, codetype, areaType, resMap);
                }
            }
//             indexservice.deletRepeatData(codetype, areaType);
        }  // 年份 for
    }



    /**
     * 处理三级数据 月度，季度 （年度另外处理） 参数不带地区。默认厦门市
     *
     * @param list
     * @param dataBase
     * @param timetype
     * @param year
     */
    public void saveNotcodeData(List<OneIndex> list, String dataBase,
                                List<String> timetype, String year,
                                String codetype, String areaType,
                                Map<String, IndexData> resMap) {
        JSONObject childObject = null;
        List<List<IndexData>> savedata = new ArrayList<>();
        Map<String, String> exCode2 = new HashMap<>();
        for (OneIndex item : list) {
            List<IndexData> LeijiList = new ArrayList<>();
            List<IndexData> dangqianList = new ArrayList<>();
            List<IndexData> ratioList = new ArrayList<>();
            List<IndexData> qimoList = new ArrayList<>();
            for (String typecode : timetype) {
                String response;
                try {
                    response = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + dataBase + "&zb=" + item.getCodeId() + "&sj=" + year + "&tt=" + typecode, 12000, 12000);
                } catch (IOException e) {
                    e.printStackTrace();
                    exCode2.put(item.getCodeId(), item.getCodeId());
                    log.info("接口： " + Until.DATA_QUERY_URL_DB_URL + "db=" + dataBase + "&zb=" + item.getCodeId() + "&sj=" + year + "&tt=" + typecode);
                    continue;
                }
                childObject = JSONObject.parseObject(response);
                int returncode = childObject.getIntValue("returncode");
                if (200 == returncode) {
                    JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                    if (childArry.size() > 0) {
                        for (int j = 0; j < childArry.size(); j++) {

                            IndexData datas4 = new IndexData();
                            JSONObject datanodes1 = (JSONObject) childArry.get(j);
                            String codes = datanodes1.getString("code");
                            CodeDataBean bean = Until.spitString(codes);
                            datas4.setCodeId(bean.getCodeId());
                            datas4.setAreaType(areaType);
                            datas4.setDatetime(bean.getDatetime());
                            datas4.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                            datas4.setCodeType(codetype);
                            JSONObject data = datanodes1.getJSONObject("data");
                            String dataValue = data.getString("data");

                            if (Until.LEIJIZHI.equals(typecode)) {
                                datas4.setCumulativeValue(dataValue);//累计值
                                LeijiList.add(datas4); //所有当前值
                            }

                            if (Until.DANGQIZHI.equals(typecode)) {
                                datas4.setCurrentValue(dataValue);//当期值
                                dangqianList.add(datas4);
                            }


                            if (Until.LEIJIZENGSU.equals(typecode)) {
                                datas4.setCumulativeRatio(dataValue);//累计增速
                                ratioList.add(datas4);
                            }
                            if (Until.QIMOZHI.equals(typecode)) {
                                datas4.setQimoValue(dataValue);//期末值
                                qimoList.add(datas4);
                            }
                        }
                    }
                }
            }
            List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
            if (tempList.size() == 1) {
                savedata.add(tempList.get(0));
            } else if (tempList.size() == 2) {
                List<IndexData> data1 = tempList.get(0);
                List<IndexData> data2 = tempList.get(1);
                for (IndexData one : data1) {
                    for (IndexData two : data2) {
                        if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                            one.setCumulativeValue(two.getCumulativeValue());
                            continue;
                        }
                    }
                }
                savedata.add(data1);

            } else if (tempList.size() == 3) {
                for (IndexData one : dangqianList) {
                    for (IndexData two : LeijiList) {
                        if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                            one.setCumulativeValue(two.getCumulativeValue());
                            continue;
                        }
                    }
                }
                for (IndexData one : dangqianList) {
                    for (IndexData two : ratioList) {
                        if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                            one.setCumulativeRatio(two.getCumulativeRatio());
                            continue;
                        }
                    }
                }
                for (IndexData one : dangqianList) {
                    if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                        for (IndexData two : qimoList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCurrentValue(two.getQimoValue());
                                continue;
                            }
                        }
                    } else {
                        break;
                    }
                }
                savedata.add(dangqianList);
            }

            Map<String, IndexData> resMap1 = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime(), two);
                        resMap1.put(two.getCodeId() + "#" + two.getDatetime(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap1.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);
        }

    }

    /**
     * 参数带地区列表  带城市
     *
     * @param list
     * @param dataBase
     * @param timetype
     * @param year
     * @param codetype
     * @param arealist
     */
    public void saveNotcodeData(List<OneIndex> list,
                                String dataBase,
                                List<String> timetype,
                                String year,
                                String codetype,
                                List<AreaCode> arealist,
                                String areaType,
                                Map<String, IndexData> resMap) {
        JSONObject childObject;
        List<List<IndexData>> savedata = new ArrayList<>();
        for (OneIndex item : list) {
            for (AreaCode areaItem : arealist) {
                List<IndexData> LeijiList = new ArrayList<>();
                List<IndexData> dangqianList = new ArrayList<>();
                List<IndexData> ratioList = new ArrayList<>();
                List<IndexData> qimoList = new ArrayList<>();

                for (String typecode : timetype) {
                    String response = null;
                    try {
                        response = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + dataBase + "&zb=" + item.getCodeId() + "&sj=" + year + "&tt=" + typecode + "&reg=" + areaItem.getAreaCode(), 65000, 65000);
                    } catch (IOException e) {
                        e.printStackTrace();
                        log.info("接口： " + Until.DATA_QUERY_URL_DB_URL + "db=" + dataBase + "&zb=" + item.getCodeId() + "&sj=" + year + "&tt=" + typecode + "&reg=" + areaItem.getAreaCode());
                        continue;
                    }
                    childObject = JSONObject.parseObject(response);
                    int returncode = childObject.getIntValue("returncode");
                    if (200 == returncode) {
                        JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                        if (childArry.size() > 0) {
                            for (int j = 0; j < childArry.size(); j++) {

                                IndexData datas4 = new IndexData();
                                JSONObject datanodes1 = (JSONObject) childArry.get(j);
                                String codes = datanodes1.getString("code");
                                CodeDataBean bean = Until.spitString(codes);
                                datas4.setCodeId(bean.getCodeId());
                                datas4.setDatetime(bean.getDatetime());
                                datas4.setAreaType(areaType);
                                datas4.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                                datas4.setCodeType(codetype);
                                JSONObject data = datanodes1.getJSONObject("data");
                                String dataValue = data.getString("data");

                                if (Until.DANGQIZHI.equals(typecode)) {
                                    datas4.setCurrentValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//当期值
                                    dangqianList.add(datas4);
                                }

                                if (Until.LEIJIZHI.equals(typecode)) {
                                    datas4.setCumulativeValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计值
                                    LeijiList.add(datas4); //所有当前值
                                }
                                if (Until.LEIJIZENGSU.equals(typecode)) {
                                    datas4.setCumulativeRatio(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计增速
                                    ratioList.add(datas4);
                                }
                                if (Until.QIMOZHI.equals(typecode)) {
                                    datas4.setQimoValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//期末值
                                    qimoList.add(datas4);
                                }

                            }
                        }
                    }
                }
                List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
                if (tempList.size() == 1) {
                    savedata.add(tempList.get(0));
                } else if (tempList.size() == 2) {
                    List<IndexData> data1 = tempList.get(0);
                    List<IndexData> data2 = tempList.get(1);
                    for (IndexData one : data1) {
                        for (IndexData two : data2) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    savedata.add(data1);

                } else if (tempList.size() == 3) {
                    for (IndexData one : dangqianList) {
                        for (IndexData two : LeijiList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        for (IndexData two : ratioList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeRatio(two.getCumulativeRatio());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                            for (IndexData two : qimoList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCurrentValue(two.getQimoValue());
                                    continue;
                                }
                            }
                        } else {
                            break;
                        }
                    }
                    savedata.add(dangqianList);
                }
            }
            Map<String, IndexData> resMap1 = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode(), two);
                        resMap1.put(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap1.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);
        }
    }


    /**
     * 保存年度数据。 带地区。(调度任务)
     * 名称为saveNianDuData实际不是保存年度数据，根据参数不同保存不同类型的数据，这里实际保存的是月度数据
     * @param db
     * @param codetype
     */
    public void saveNianDuData(String db, String codetype, String areaType, Map<String, String> areaMap) {


        List<OneIndex> index2s = oneService.getTwoNode(codetype, "2", areaType); //获取所有二级指标
        //全国月度有四级指标
        if ("N".equals(codetype) || "Y".equals(codetype) && "G".equals(areaType)) {
            List<OneIndex> oneIndices = oneService.getTwoNode(codetype, "3", areaType); //获取所有三级指标
            index2s.addAll(oneIndices);
        }

//        List<OneIndex> index2s = new ArrayList<>();
//        OneIndex x = new OneIndex();
//        //区级年度  区级年度常住人口 A030101
//        x.setCodeId("A030101");
//        index2s.add(x);
//
//        OneIndex x1 = new OneIndex();
//        //区级年度  区级年度户籍人口项下 A030101
//        x1.setCodeId("A0302");
//        index2s.add(x1);

        List<String> timeTypelist = Until.TimeTypes();

        String yearresponse = null;
        try {
            yearresponse = Until.HttpGET(Until.GET_DATA_TIME + "db=" + db, 65000, 65000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.GET_DATA_TIME + "db=" + db);
        }

        List<String> yearcodes = new ArrayList<>();
        JSONObject obj = JSONObject.parseObject(yearresponse);
        if (200 == obj.getIntValue("returncode")) {
            String year = "";
            JSONArray array = obj.getJSONArray("returndata");
            if (array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    year = ((JSONObject) array.get(i)).getString("code");
                    year = year.substring(0, 4);
                    yearcodes.add(year);
                }
            }
        }
        yearcodes = removeDuplicate(yearcodes);
//        List<String> yearList = yearcodes;                                        //所有年份
        List<String> yearList = Collections.singletonList(yearcodes.get(0));    //最新一年
        for (String yearcode : yearList) {
            log.info("-------第" + yearcode + "--------------");
            JSONObject childObject = null;
            List<List<IndexData>> savedata = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> map2 = new HashMap<>();
            List<AreaCode> areas = getArea(db, areaType);
            for (OneIndex item2 : index2s) {
                for (AreaCode areaitem : areas) {
                    List<IndexData> LeijiList = new ArrayList<>();
                    List<IndexData> dangqianList = new ArrayList<>();
                    List<IndexData> ratioList = new ArrayList<>();
                    List<IndexData> qimoList = new ArrayList<>();
                    List<IndexData> tongbiList = new ArrayList<>();
                    List<IndexData> tongbiList1 = new ArrayList<>();

                    for (int i = 0; i < timeTypelist.size(); i++) //当期值，累计值，增速
                    {
                        String typecode = timeTypelist.get(i);
                        String childResponse = null;
                        try {
                            childResponse = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode + "&reg=" + areaitem.getAreaCode(), 65000, 65000);
                        } catch (IOException e) {
                            e.printStackTrace();
                            log.info("接口：" + Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode + "&reg=" + areaitem.getAreaCode());
                            continue;
                        }
                        childObject = JSONObject.parseObject(childResponse);
                        int returnCode = childObject.getIntValue("returncode");
                        if (returnCode == 200) {
                            JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                            if (childArry.size() > 0) {
                                for (int j = 0; j < childArry.size(); j++) {
                                    IndexData datas = new IndexData();
                                    JSONObject datanodes = (JSONObject) childArry.get(j);
                                    String codes = datanodes.getString("code");
                                    CodeDataBean bean = Until.spitString(codes);
                                    datas.setDatetime(bean.getDatetime());
                                    datas.setCodeId(bean.getCodeId());
                                    datas.setAreaType(areaType);
                                    datas.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                                    datas.setAreaName(areaMap.get(datas.getAreaCode()));
                                    datas.setCodeType(codetype);
                                    JSONObject data = datanodes.getJSONObject("data");
                                    String dataValue = data.getString("data");


                                    if (Until.DANGQIZHI.equals(typecode)) {
                                        datas.setCurrentValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//当期值
                                        dangqianList.add(datas);
                                    }

                                    if (Until.LEIJIZHI.equals(typecode)) {
                                        datas.setCumulativeValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计值
                                        LeijiList.add(datas); //所有当前值
                                    }
                                    if (Until.LEIJIZENGSU.equals(typecode)) {
                                        datas.setCumulativeRatio(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计增速
                                        ratioList.add(datas);
                                    }
                                    if (Until.QIMOZHI.equals(typecode)) {
                                        datas.setQimoValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//期末值
                                        qimoList.add(datas);
                                    }

                                    if (Until.TONGBIZZ.equals(typecode)) {
                                        datas.setTongbiValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//比上年同期增长率
                                        tongbiList.add(datas);
                                    }

                                    if (Until.BI_SHAGN_NIAN.equals(typecode)) {
                                        datas.setTongbiValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//比上年同期增长率
                                        tongbiList1.add(datas);
                                    }
                                }
                            }
                        } else {
                            map.put(item2.getCodeId(), item2.getCodeId());
                            log.info("未处理的code: " + item2.getCodeId());
                        }
                    }
                    List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
                    if (tempList.size() == 1) {
                        savedata.add(tempList.get(0));
                    } else if (tempList.size() == 2) {
                        List<IndexData> data1 = tempList.get(0);
                        List<IndexData> data2 = tempList.get(1);
                        for (IndexData one : data1) {
                            for (IndexData two : data2) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                    one.setCumulativeValue(two.getCumulativeValue());
                                    continue;
                                }
                            }
                        }
                        savedata.add(data1);
                    } else if (tempList.size() == 3) {
                        for (IndexData one : dangqianList) {
                            for (IndexData two : LeijiList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                    one.setCumulativeValue(two.getCumulativeValue());
                                    continue;
                                }
                            }
                        }
                        for (IndexData one : dangqianList) {
                            for (IndexData two : ratioList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                    one.setCumulativeRatio(two.getCumulativeRatio());
                                    continue;
                                }
                            }
                        }

                        for (IndexData one : dangqianList) {
                            if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                                for (IndexData two : qimoList) {
                                    if (!"0".equals(two.getQimoValue()) && one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                        one.setCurrentValue(two.getQimoValue());
                                        continue;
                                    }
                                }
                            }
                        }
                        for (IndexData one : dangqianList) {
                            if ("0".equals(one.getCumulativeValue()) && qimoList.size() > 0) {
                                for (IndexData two : qimoList) {
                                    if (!"0".equals(two.getQimoValue()) && one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                        one.setCumulativeValue(two.getQimoValue());
                                        continue;
                                    }
                                }
                            }
                        }

                        for (IndexData one : dangqianList) {
                            if ("0".equals(one.getCumulativeRatio()) && tongbiList.size() > 0) {
                                for (IndexData two : tongbiList) {
                                    if (!"0".equals(two.getTongbiValue()) && one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                        one.setCumulativeRatio(two.getTongbiValue());
                                        continue;
                                    }
                                }
                            }
                        }

                        for (IndexData one : dangqianList) {
                            if ("0".equals(one.getCumulativeRatio()) && tongbiList1.size() > 0) {
                                for (IndexData two : tongbiList1) {
                                    if (!"0".equals(two.getTongbiValue()) && one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                        one.setCumulativeRatio(two.getTongbiValue());
                                        continue;
                                    }
                                }
                            }
                        }
                        savedata.add(dangqianList);
                    }
                }
            }
            deleteDatas(yearcode, codetype, areaType);//删除历史数据
            Map<String, IndexData> resMap = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);
            //处理三级数据
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                List<OneIndex> remains = oneService.getChileNode(entry.getValue().toString(), codetype, areaType);
                if (ObjectUtils.isNotBlank(remains) && remains.size() > 0) {
                    saveNotcodeData(remains, db, timeTypelist, yearcode, codetype, areas, areaType, resMap);
                }
            }
            //   indexservice.deletRepeatData(codetype, areaType);
        }

    }

    //**父节点**
    public List getArea(String db, String areaType) {
        JSONArray array = null;
        JSONObject areaObject = null;
        List<AreaCode> arealist = new ArrayList<>();

        if (JIDU_QUANGUO.equals(db) || NIANDU_QUANGUO.equals(db) || YUEDU_QUANGUO.equals(db)) {
            AreaCode areas = new AreaCode();
            areas.setAreaCode("GX000000");
            areas.setAreaName("全国");
            areas.setAreaType(areaType);
            arealist.add(areas);
            return arealist;
        }

        String response = null;
        try {
            response = Until.HttpGET(Until.AREA_CODE_URL_DB_URL + "db=" + db, 12000, 6000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.AREA_CODE_URL_DB_URL + "db=" + db);
        }
        JSONObject jsonObject = JSONObject.parseObject(response);
        int returncode = jsonObject.getIntValue("returncode");
        if (returncode == 200) {
            array = jsonObject.getJSONArray("returndata");
            for (int i = 0; i < array.size(); i++) {
                AreaCode areas = new AreaCode();
                areaObject = (JSONObject) array.get(i);
                areas.setAreaCode(areaObject.getString("code"));
                areas.setAreaName(areaObject.getString("name"));
                areas.setAreaType(areaType);
                arealist.add(areas);
            }
        }
        return arealist;

    }

    public void saveArea(List<AreaCode> listarea, String db) {
        JSONArray array = null;
        JSONObject areaObject = null;
        List<AreaCode> arealist = new ArrayList<>();
        for (AreaCode areaItem : listarea) {
            String response = null;
            try {
                response = Until.HttpGET(Until.AREA_CODE_URL_DB_URL + "db=" + db + "&p=" + areaItem.getAreaCode(), 12000, 6000);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("接口：" + Until.AREA_CODE_URL_DB_URL + "db=" + db + "&p=" + areaItem.getAreaCode());
            }
            JSONObject jsonObject = JSONObject.parseObject(response);
            int returncode = jsonObject.getIntValue("returncode");
            if (200 == returncode) {
                array = jsonObject.getJSONArray("returndata");
                for (int i = 0; i < array.size(); i++) {
                    AreaCode areas = new AreaCode();
                    areaObject = (JSONObject) array.get(i);
                    areas.setParentCode(areaItem.getAreaCode());
                    areas.setAreaCode(areaObject.getString("code"));
                    areas.setAreaName(areaObject.getString("name"));
                    // areas.setParentCode(areaItem.getAreaCode());
                    arealist.add(areas);
                }
            }
        }
        areaService.batchSave(arealist);
    }

    /**
     * 更新解释 和单位
     *
     * @param db
     * @param codeType
     * @return
     */
    public void updateIndexInfo(String db, String codeType) {
        JSONObject indexInfo = null;
        String areaType = "";
        if (JIDU_9DI.endsWith(db) || NIANDU_9DI.equals(db) || YUEDU_9DI.equals(db)) {
            areaType = "E";
        } else if (JIDU_FUSHENGJI15.endsWith(db) || NIANDU_FUSHENGJI15.equals(db) || YUEDU_FUSHENGJI15.equals(db)) {
            areaType = "C";
        } else if (JIDU_TEQU4.endsWith(db) || NIANDU_TEQU4.equals(db) || YUEDU_TEQU4.equals(db)) {
            areaType = "B";
        } else if (JIDU_QUANGUO.endsWith(db) || NIANDU_QUANGUO.equals(db) || YUEDU_QUANGUO.equals(db)) {
            areaType = "G";
        } else if (JIDU_QUJI.endsWith(db) || NIANDU_QUJI.equals(db) || YUEDU_QUJI.equals(db)) {
            areaType = "A";
        } else if (JIDU_SHIJI.endsWith(db) || NIANDU_SHIJI.equals(db) || YUEDU_SHIJI.equals(db)) {
            areaType = "D";
        } else {
            areaType = "S";
        }
        List<OneIndex> indexlist = oneService.getAllIndexs(codeType, areaType);

        for (OneIndex item : indexlist) {
            String response = null;
            try {
                response = Until.HttpGET(Until.GET_DATA_INFO + "&db=" + db + "&code=" + item.getCodeId(), 24000, 24000);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("接口：" + Until.GET_DATA_INFO + "&db=" + db + "&code=" + item.getCodeId());
            }
            if (ObjectUtils.isBlank(response)) {
                continue;
            }
            JSONObject obj = JSONObject.parseObject(response);
            int returncode = obj.getIntValue("returncode");
            if (200 == returncode) {
                // OneIndex index    = new OneIndex();
                indexInfo = obj.getJSONObject("returndata");
                // index.setCodeId(indexInfo.getString("code"));
                item.setUnit(indexInfo.getString("unitname"));
                //indexlist.add(index);
            }
            oneService.update(item);
        }

        System.out.println("更新成功");

    }


    /**
     * 定时任务
     */
    public void timeclock() {
        log.info("----------------------------计时器开始: " + Until.getCurrentDateTime() + "-------------------------------");
        //Y月度 J 季度 N 年度
        // A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国
        /*//1.更新地区
        this.savaAreaCode();*/
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        String codetype;
        String areaType;
        //到时候代码还原
        /*saveYueDuData(Until.YUEDU_SHIJI, codetype, areaType);
        log.info("---------------月度数据保存成功 " + Until.getCurrentDateTime() + "----------");
        log.info("--------区级别数据 月度 " + Until.getCurrentDateTime() + "--------");
        codetype = "Y";
        areaType = "A";
        saveNianDuData(Until.YUEDU_QUJI, codetype, areaType, areaMap);
        log.info("---------区级别数据月度数据保存成功 " + Until.getCurrentDateTime());*/
        //到时候代码还原


        System.out.println(Until.getCurrentDateTime());
        log.info("----------------------------计时器开始: " + Until.getCurrentDateTime() + "-------------------------------");

        log.info("--------------------------全省数据任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------全省数据 月度 " + Until.getCurrentDateTime() + "--------");
        codetype = "Y";
        areaType = "P";
//        listmap = getYueDuDataFromURL(Until.YUEDU_QS, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.YUEDU_QS, codetype);
        saveYueDuData(Until.YUEDU_QS, codetype, areaType);
        log.info("---------全省数据月度数据保存成功 " + Until.getCurrentDateTime());

        log.info("--------------------------区级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
//      区级月度可以
        log.info("--------区级别数据 月度 " + Until.getCurrentDateTime() + "--------");
        codetype = "Y";
        areaType = "A";
//        listmap = getYueDuDataFromURL(Until.YUEDU_QUJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.YUEDU_QUJI, codetype);
        saveNianDuData(Until.YUEDU_QUJI, codetype, areaType, areaMap);
        log.info("---------区级别数据月度数据保存成功 " + Until.getCurrentDateTime());

//        log.info("--------区级别数据 季度 " + Until.getCurrentDateTime() + "--------");
//        codetype = "J";
//        areaType = "A";
////        listmap = getYueDuDataFromURL(Until.JIDU_QUJI, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.JIDU_QUJI, codetype);
//        saveNianDuData(Until.JIDU_QUJI, codetype, areaType, areaMap);
//        log.info("---------区级别数据季度数据保存成功 " + Until.getCurrentDateTime());
//
//        区级年度可以
//        log.info("--------区级别数据 年度 " + Until.getCurrentDateTime() + "--------");
//        codetype = "N";
//        areaType = "A";
////        listmap = getYueDuDataFromURL(Until.NIANDU_QUJI, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.NIANDU_QUJI, codetype);
//        saveNianDuData(Until.NIANDU_QUJI, codetype, areaType, areaMap);
//        log.info("---------区级别数据年度数据保存成功 " + Until.getCurrentDateTime());
//
//        log.info("--------------------------区级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
//
        log.info("--------------------------四个经济特区任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
//        四个经济特区月度可以
        log.info("-------四个经济特区 月度 " + Until.getCurrentDateTime() + "------------");
        codetype = "Y";
        areaType = "B";
//        listmap = getYueDuDataFromURL(Until.YUEDU_TEQU4, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.YUEDU_TEQU4, codetype);
        saveNianDuData(Until.YUEDU_TEQU4, codetype, areaType, areaMap);
        log.info("---------四个经济特区 月度数据保存成功 " + Until.getCurrentDateTime());

//        log.info("-------四个经济特区 季度 " + Until.getCurrentDateTime() + "------------");
//        codetype = "J";
//        areaType = "B";
//        listmap = getYueDuDataFromURL(Until.JIDU_TEQU4, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_TEQU4, codetype);
//        saveNianDuData(Until.JIDU_TEQU4, codetype, areaType, areaMap);
//
//        log.info("-------四个经济特区 年度 " + Until.getCurrentDateTime() + "------------");
//        codetype = "N";
//        areaType = "B";
//        listmap = getYueDuDataFromURL(Until.NIANDU_TEQU4, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_TEQU4, codetype);
//        saveNianDuData(Until.NIANDU_TEQU4, codetype, areaType, areaMap);
//        log.info("--------------------------四个经济特区任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
//
        log.info("--------------------------十五个副省级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
//      十五个副省级可以
        log.info("---------十五个副省级 月度" + Until.getCurrentDateTime() + "-----------");
        codetype = "Y";
        areaType = "C";
//        listmap = getYueDuDataFromURL(Until.YUEDU_FUSHENGJI15, "Y", areaType);
//        saveIndexs(listmap, "Y", areaType);
//        updateIndexInfo(Until.YUEDU_FUSHENGJI15, "Y");
        saveNianDuData(Until.YUEDU_FUSHENGJI15, codetype, areaType, areaMap);
        log.info("---------十五个副省级月度数据保存成功 " + Until.getCurrentDateTime());

//        log.info("---------十五个副省级 季度" + Until.getCurrentDateTime() + "-----------");
//        codetype = "J";
//        areaType = "C";
////        listmap = getYueDuDataFromURL(Until.JIDU_FUSHENGJI15, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.JIDU_FUSHENGJI15, codetype);
//        saveNianDuData(Until.JIDU_FUSHENGJI15, codetype, areaType, areaMap);
//        log.info("---------十五个副省级季度数据保存成功 " + Until.getCurrentDateTime());
//
//        log.info("---------十五个副省级 年度" + Until.getCurrentDateTime() + "-----------");
//        codetype = "N";
//        areaType = "C";
////        listmap = getYueDuDataFromURL(Until.NIANDU_FUSHENGJI15, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.NIANDU_FUSHENGJI15, codetype);
//        saveNianDuData(Until.NIANDU_FUSHENGJI15, codetype, areaType, areaMap);
//        log.info("---------十五个副省级 年度数据保存成功 " + Until.getCurrentDateTime());
//
//        log.info("--------------------------十五个副省级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
//
        log.info("--------------------------市级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
//      市级月度可以
        codetype = "Y";
        areaType = "D";
//        listmap = getYueDuDataFromURL(Until.YUEDU_SHIJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.YUEDU_SHIJI,codetype);
        saveYueDuData(Until.YUEDU_SHIJI, codetype, areaType);
        log.info("---------------月度数据保存成功 " + Until.getCurrentDateTime() + "----------");
//        areaType = "D";
//        codetype = "J";
////        listmap = getYueDuDataFromURL(Until.JIDU_SHIJI, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.JIDU_SHIJI, codetype);
//        saveYueDuData(Until.JIDU_SHIJI, codetype, areaType);
//        log.info("---------季度数据保存成功 " + Until.getCurrentDateTime());
//        areaType = "D";
//        codetype = "N";
////        listmap = getYueDuDataFromURL(Until.NIANDU_SHIJI, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.NIANDU_SHIJI, codetype);
//        saveYueDuData(Until.NIANDU_SHIJI, codetype, areaType);
//        log.info("---------年度数据保存成功 " + Until.getCurrentDateTime());

//        log.info("--------------------------市级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
//
        log.info("--------------------------九地市开始: " + Until.getCurrentDateTime() + "-------------------------------");
//        九地市 月度 可以
        log.info("---------九地市 月度 " + Until.getCurrentDateTime() + "------------");
        codetype = "Y";
        areaType = "E";
//        listmap = getYueDuDataFromURL(Until.YUEDU_9DI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.YUEDU_9DI, codetype);
        saveNianDuData(Until.YUEDU_9DI, codetype, areaType, areaMap);
        log.info("---------九地市月度数据保存成功 " + Until.getCurrentDateTime());
//
//        log.info("--------九地市 季度 " + Until.getCurrentDateTime() + "------------");
//        codetype = "J";
//        areaType = "E";
////        listmap = getYueDuDataFromURL(Until.JIDU_9DI, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.JIDU_9DI, codetype);
//        saveNianDuData(Until.JIDU_9DI, codetype, areaType, areaMap);
//        log.info("---------九地市季度数据保存成功 " + Until.getCurrentDateTime());
//
//        log.info("--------九地市 年度 " + Until.getCurrentDateTime() + "------------");
//        codetype = "N";
//        areaType = "E";
////        listmap = getYueDuDataFromURL(Until.NIANDU_9DI, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.NIANDU_9DI, codetype);
//        saveNianDuData(Until.NIANDU_9DI, codetype, areaType, areaMap);
//        log.info("---------九地市年度保存成功 " + Until.getCurrentDateTime());
//
//        log.info("--------------------------九地市结束: " + Until.getCurrentDateTime() + "-------------------------------");
//
        log.info("--------------------------全国任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------全国 月度 " + Until.getCurrentDateTime() + "------------");
        //      全国 月度可以
        codetype = "Y";
        areaType = "G";
//        listmap = getYueDuDataFromURL(Until.YUEDU_QUANGUO, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.YUEDU_QUANGUO, codetype);
        saveNianDuData(YUEDU_QUANGUO, codetype, areaType, areaMap);
        log.info("---------全国月度保存成功 " + Until.getCurrentDateTime());
//
//        log.info("-------全国 季度 " + Until.getCurrentDateTime() + "------------");
//        codetype = "J";
//        areaType = "G";
////        listmap = getYueDuDataFromURL(Until.JIDU_QUANGUO, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.JIDU_QUANGUO, codetype);
//        saveNianDuData(Until.JIDU_QUANGUO, codetype, areaType, areaMap);
//        log.info("---------全国季度保存成功 " + Until.getCurrentDateTime());
//
//        log.info("-------全国 年度 " + Until.getCurrentDateTime() + "------------");
//        codetype = "N";
//        areaType = "G";
//        listmap = getYueDuDataFromURL(Until.NIANDU_QUANGUO, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.NIANDU_QUANGUO, codetype);
////        saveNianDuData(Until.NIANDU_QUANGUO, codetype, areaType, areaMap);
//        log.info("---------全国年度保存成功 " + Until.getCurrentDateTime());
//
//        log.info("--------------------------全国任务结束: " + Until.getCurrentDateTime() + "-------------------------------");


//跑到这里

//        log.info("---------首页 月度 " + Until.getCurrentDateTime() + "------------");
//        areaType = "S";
//        codetype = "Y";
////        Map<String, Object> listmap = getYueDuDataFromURL(Until.YEUDU_SHOUYE, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.YEUDU_SHOUYE,codetype);
////        saveNianDuData(Until.YEUDU_SHOUYE, codetype, areaType, areaMap);
//        saveYueDuData(Until.YEUDU_SHOUYE, codetype, areaType);
//        log.info("---------首页 月度更新成功" + Until.getCurrentDateTime() + "------------");

        //更新用户表
        this.updateUserTable();
        log.info("--------------------------计时器结束： " + Until.getCurrentDateTime() + "-------------------------------");

    }


    /**
     * 删除数据 不带codeId
     *
     * @param yearcode
     * @param codetype
     * @param areatype
     */
    public void deleteDatas(String yearcode, String codetype, String areatype) {

        indexservice.deleteIndexData(yearcode, codetype, areatype);
    }

    /**
     * 删除数据 带codeId
     *
     * @param yearcode
     * @param codetype
     * @param areatype
     */
    public void deleteDatas(String yearcode, String codetype, String areatype, String codeId) {

        indexservice.deleteIndexData(yearcode, codetype, areatype, codeId);
    }


    /**
     * 保存地区编码数据
     */
    public void savaAreaCode() {
        //删除所有地区数据
        areaService.deleteAreaCode();
        //全国月度数据（APP）
        getDataProcessService.saveDataReg(YUEDU_QUANGUO, "G");
        //区级月度数据（APP）
        getDataProcessService.saveDataReg(Until.YUEDU_QUJI, "A");
        //四个经济特区月度数据（APP）
        getDataProcessService.saveDataReg(Until.YUEDU_TEQU4, "B");
        //9个设区市月度数据（APP）
        getDataProcessService.saveDataReg(Until.YUEDU_9DI, "E");
        //十五个副省级月度数据（APP）
        getDataProcessService.saveDataReg(Until.YUEDU_FUSHENGJI15, "C");
    }

    /**
     * 更新用户表
     */
    public void updateUserTable() {
        log.info("--------------------------更新用户列表开始： " + Until.getCurrentDateTime() + "-------------------------------");
        UserInfoController userInfoController = new UserInfoController();
        //从第三方获取所有用户信息
        List<UserInfo> remoteList = service.callBackUsers();
        //获取本地所有用户信息
        List<UserInfo> localList = service.queryAllUserInfo();
        int u1 = localList.size();
        //本地用户键值对
        Map<String, UserInfo> localUsersMap = new HashMap<>();
        for (UserInfo userInfo : localList) {
            localUsersMap.put(userInfo.getUserId(), userInfo);
        }
        List<UserInfo> needUpdateUsers = new ArrayList<>();
        List<UserInfo> needInsertUsers = new ArrayList<>();
        for (UserInfo user : remoteList) {
            //如果本地用户表已经存在第三方的用户，则进行更新。否则进行新增
            if (localUsersMap.containsKey(user.getUserId())) {
                UserInfo newUserInfo = localUsersMap.get(user.getUserId());
                //更新用户信息
                newUserInfo.setUserId(user.getUserId());
                newUserInfo.setUserName(user.getUserName());
                newUserInfo.setSex(user.getSex());
                newUserInfo.setTel(user.getTel());
                newUserInfo.setDeptCode(user.getDeptCode());
                newUserInfo.setEmail(user.getEmail());
                newUserInfo.setDuty(user.getDuty());
                newUserInfo.setUpdatetime(user.getUpdatetime());
                newUserInfo.setErrCount(user.getErrCount());
                newUserInfo.setUpdateUserid(user.getUpdateUserid());
                newUserInfo.setRoles(user.getRoles());
                needUpdateUsers.add(newUserInfo);
            } else {
                needInsertUsers.add(user);
            }
        }
        //进行批量新增
        service.batchSave(needInsertUsers);
        //进行批量更新
        service.batchUpdate(needUpdateUsers);
        log.info("--------------------------用户信息列表更新成功" + Until.getCurrentDateTime() + "-------------------------------");
        //查询所有用户
        List<UserInfo> allUserInfo = service.queryAllUserInfo();
        List<UserInfo> usersUpdate = new ArrayList<>();
        for (UserInfo userInfo : allUserInfo) {
            //不需要传token
            String userId = userInfo.getUserId();
            String url = "userid=" + userId;
            String norights = null;
            try {
                norights = Until.HttpGET(Until.GET_NORIGHTLIST + url, 5000, 5000);
            } catch (IOException e) {
                log.info("接口: " + Until.GET_NORIGHTLIST + url);
                e.printStackTrace();
                continue;
            }
            JSONArray jsonArray = JSONObject.parseArray(norights);
            String noRightResult = jsonArray.toString();
            if (StringUtils.isBlank(noRightResult)) {
                noRightResult = "[]";
            }
            userInfo.setNoRight(noRightResult);
            usersUpdate.add(userInfo);
        }
        service.batchUpdate(usersUpdate);
        log.info("权限插入成功");
        //从第三方获取所有用户信息
        List<UserInfo> remoteList1 = service.callBackUsers();
        //查询所有用户
        List<UserInfo> allUserInfo1 = service.queryAllUserInfo();
        Map<String, String> norightMap = new HashMap<>();
        for (UserInfo userInfo : remoteList1) {
            //不需要传token
            String userId = userInfo.getUserId();
            String url = "userid=" + userId;
            String norights = null;
            try {
                norights = Until.HttpGET(Until.GET_NORIGHTLIST + url, 5000, 5000);
            } catch (IOException e) {
                log.info("接口: " + Until.GET_NORIGHTLIST + url);
                e.printStackTrace();
                continue;
            }
            String noRightResult = norights;
            if (StringUtils.isBlank(noRightResult)) {
                noRightResult = "[]";
            }
            norightMap.put(userInfo.getUserId(), noRightResult);
        }
        List<UserInfo> norightNeedUpdate = new ArrayList<>();
        for (UserInfo u : allUserInfo1) {
            String userId = u.getUserId();
            if (norightMap.containsKey(userId)) {
                u.setNoRight(norightMap.get(userId));
                norightNeedUpdate.add(u);
            }
        }
        service.batchUpdate(norightNeedUpdate);
        log.info("权限更新成功");
        log.info("--------------------------更新用户列表结束： " + Until.getCurrentDateTime() + "-------------------------------");
    }
}
